//
//  Created by  fred on 2016/10/26.
//  Copyright © 2016年 Alibaba. All rights reserved.
//

package com.zmzncs.lmtc.common.interactive.miwang;

import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;




@Slf4j
public class MiWangParkOpenapi {

    private static String appKey = "203873946";

    private static String appSecret = "viSp5zjXD2VIx0W7HPHZgNYOivxK34Jh";

    // private static String partnerCode = "0571105";

    private static String partnerCode = "0311103";

    static{
        HttpClientBuilderParams httpParam = new HttpClientBuilderParams();
        httpParam.setAppKey(appKey);
        httpParam.setAppSecret(appSecret);
        HttpApiClientMWpark.getInstance().init(httpParam);
    }

    /**
     * 查询停车场信息
     * @return
     */
    public static String  mwpark_sandbox_getParkingLotByCodeHttpSync(String partnerCode){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_getParkingLotByCodeSyncMode(appKey , partnerCode);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("查询停车场信息 mwpark_sandbox_getParkingLotByCodeHttpSync error:",ex);
        }
        return resultStr;
    }

    /**
     * 查询停车场出入口及车道集合
     * @return
     */
    public static String mwpark_sandbox_queryEntranceAndRoadWayHttpSync(String partnerCode){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_queryEntranceAndRoadWaySyncMode(appKey , partnerCode);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("查询停车场出入口及车道集合 mwpark_sandbox_queryEntranceAndRoadWayHttpSync error:",ex);
        }
        return resultStr;
    }

    /**
     * 临时车实时计价查询
     */
    public static String  mwpark_sandbox_getCurrentChargeBillHttpSync(String partnerCode,String carNum){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_getCurrentChargeBillSyncMode(appKey , partnerCode , "" , carNum , "");
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("临时车实时计价查询 error:",ex);
        }
        return resultStr;
    }
    /**
     * 无牌车实时计价查询
     */
    public static String mwpark_sandbox_getTempoarnyCurrentChargeBillHttpSync(String partnerCode,String roadWayCode ,String tempoarnyCode ){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_getCurrentChargeBillSyncMode(appKey , partnerCode , roadWayCode, "" , tempoarnyCode);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("无牌车实时计价查询 error:",ex);
        }
        return resultStr;
    }

    /**
     * 支付完成后，回单接口
     */
    public static String mwpark_sandbox_rechargeNotifyHttpSync(String partnerCode, String billCode , Double payAmont ,String outTradeCode){
        String resultStr = "";
        //appKey
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_rechargeNotifySyncMode( appKey, partnerCode, billCode , payAmont , outTradeCode);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("支付完成后，回单接口 error:",ex);
        }
        return resultStr;
    }


    /**
     * 包月卡分租接口
     */
    public static String mwpark_sandbox_queryFixedCarRuleHttpSync(String partnerCode){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_queryFixedCarRuleSyncMode(appKey , partnerCode);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("包月卡分租接口 error:",ex);
        }
        return resultStr;
    }


    /**
     * 包月车查询接口
     */
    public static String mwpark_sandbox_queryFixedCarHttpSync(String partnerCode,String plateNumber){
        String resultStr = "";
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_queryFixedCarSyncMode(appKey , partnerCode , DateTimeUtil.getAfterDayDateHMS("-1") , DateTimeUtil.getAfterDayDateHMS("1") , null, plateNumber , null, null, 1 , 1 , null , null , null);
        try {
            return getResultString(response);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("包月车查询接口 error:",ex);
        }
        return resultStr;
    }





    public static void mwpark_openapi_roadWayControlGateHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_openapi_roadWayControlGate("default" , "default" , "default" , 1 , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_openapi_roadWayControlGateHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_openapi_roadWayControlGateSyncMode("default" , "default" , "default" , 1);
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }



    public static void mwpark_sandbox_queryEntranceAndRoadWayHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_queryEntranceAndRoadWay("default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }



    public static void mwpark_sandbox_getParkingLotByCodeHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_getParkingLotByCode("203873946" , "0571105" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }




    public static void mwpark_sandbox_incrementFixedCarSyncHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_incrementFixedCarSync("default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_sandbox_incrementFixedCarSyncHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_incrementFixedCarSyncSyncMode("default" , "default");
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }



    public static void mwpark_sandbox_deleteFixedCar_batchHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_deleteFixedCar_batch("default" , "default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_sandbox_deleteFixedCar_batchHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_deleteFixedCar_batchSyncMode("default" , "default" , "default");
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }



    public static void mwpark_sandbox_updateFixedCarHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_updateFixedCar("default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , 1.0 , 1 , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_sandbox_updateFixedCarHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_updateFixedCarSyncMode("default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , 1.0 , 1 , "default");
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }



    public static void mwpark_sandbox_insertOrRenewFixedCarHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_insertOrRenewFixedCar("default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , 1.0 , 1 , "default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_sandbox_insertOrRenewFixedCarHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_insertOrRenewFixedCarSyncMode("default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , 1.0 , 1 , "default" , "default");
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }



    public static void mwpark_sandbox_queryFixedCarHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_queryFixedCar("default" , "default" , "default" , "default" , "default" , "default" , "default" , "default" , 1 , 1 , "default" , "default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }


    public static void mwpark_sandbox_queryFixedCarRuleHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_queryFixedCarRule("default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }




    public static void mwpark_sandbox_rechargeNotifyHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_rechargeNotify("default" , "default" , "default" , 1.0 , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }





    public static void mwpark_sandbox_getCurrentChargeBillHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_getCurrentChargeBill("default" , "default" , "default" , "default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }




    public static void mwpark_sandbox_scanInRoadWayCodeHttpTest(){
        HttpApiClientMWpark.getInstance().mwpark_sandbox_scanInRoadWayCode("default" , "default" , "default" , new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                try {
                    System.out.println(getResultString(response));
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void mwpark_sandbox_scanInRoadWayCodeHttpSyncTest(){
        ApiResponse response = HttpApiClientMWpark.getInstance().mwpark_sandbox_scanInRoadWayCodeSyncMode(appKey, "default" , "default");
        try {
            System.out.println(getResultString(response));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }




    public static String getResultString(ApiResponse response) throws IOException {
        StringBuilder result = new StringBuilder();
        result.append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("Response from backend server")
        result.append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("ResultCode:").append(response.getCode())
        if(response.getCode() != 200){
            result.append(response.getHeaders().get("X-Ca-Error-Message")).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("Error description:")
        }

        result.append(SdkConstant.CLOUDAPI_LF).append(new String(response.getBody() , SdkConstant.CLOUDAPI_ENCODING)); //.append("ResultBody:")

        return result.toString();
    }



    public static String getResultString02(ApiResponse response) throws IOException {
        StringBuilder result = new StringBuilder();
        result.append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("Response from backend server")
        result.append(SdkConstant.CLOUDAPI_LF).append(response.getCode()).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("ResultCode:")
        if(response.getCode() != 200){
            result.append(response.getHeaders().get("X-Ca-Error-Message")).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF); //.append("Error description:")
        }

        result.append(SdkConstant.CLOUDAPI_LF).append(new String(response.getBody() , SdkConstant.CLOUDAPI_ENCODING)); //.append("ResultBody:")

        return result.toString();
    }

}
